//
//  PublishListCell.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/25.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "PublishListCell.h"

@interface PublishListCell ()

@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIButton *collectionBtn;
@property (nonatomic, strong) UIButton *commentBtn;
@property (nonatomic, strong) UIButton *shareBtn;

@property (nonatomic, strong) UILabel *noteLabel;;

@end

@implementation PublishListCell

+ (PublishListCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"PublishListCell";
    PublishListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[PublishListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
        [cell createSubView];
    }
    
    return cell;
}

- (void)createSubView {
    self.noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(24), ScreenX375(220), ScreenX375(300), ScreenX375(18))];
    self.noteLabel.textColor = FONTCOLOR_BLACK;
    self.noteLabel.font = Kfont(16);
    self.noteLabel.text = @"设计生活，设计美，独特创意";
    [self addSubview:self.noteLabel];
    
    UIView *shadowBg = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(24), ScreenX375(20), (SCREEN_WIDTH - ScreenX375(48)), ScreenX375(185))];
    shadowBg.layer.cornerRadius = ScreenX375(8);
    shadowBg.layer.shadowColor = RGB_COLOR(116, 116, 116).CGColor;
    shadowBg.layer.shadowOpacity = 0.07;
    shadowBg.layer.shadowOffset = CGSizeMake(0, 8);
    shadowBg.layer.shadowRadius = 9;
    [self addSubview:shadowBg];
    
    self.bgView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(24), ScreenX375(20), (SCREEN_WIDTH - ScreenX375(48)), ScreenX375(185))];
    self.bgView.image = [UIImage imageNamed:@"图层57"];
    self.bgView.contentMode = UIViewContentModeScaleAspectFill;
    self.bgView.layer.cornerRadius = ScreenX375(8);
    self.bgView.clipsToBounds = YES;
    [self addSubview:self.bgView];
    
    self.collectionBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(12), ScreenX375(250), ScreenX375(60), ScreenX375(15))];
    [self.collectionBtn setImage:ImageWithName(@"点赞") forState:UIControlStateNormal];
    [self.collectionBtn setTitle:@" 665" forState:UIControlStateNormal];
    [self.collectionBtn setTitleColor:FONTCOLOR_BLACK forState:UIControlStateNormal];
    self.collectionBtn.titleLabel.font = Kfont(12);
    [self.collectionBtn addTarget:self action:@selector(likeAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.collectionBtn];
    
    self.commentBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(65), ScreenX375(250), ScreenX375(60), ScreenX375(15))];
    [self.commentBtn setImage:ImageWithName(@"评论") forState:UIControlStateNormal];
    [self.commentBtn setTitle:@" 26" forState:UIControlStateNormal];
    [self.commentBtn setTitleColor:FONTCOLOR_BLACK forState:UIControlStateNormal];
    self.commentBtn.titleLabel.font = Kfont(12);
    [self.commentBtn addTarget:self action:@selector(commentAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.commentBtn];
    
    self.shareBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(113), ScreenX375(250), ScreenX375(60), ScreenX375(15))];
    [self.shareBtn setImage:ImageWithName(@"分享") forState:UIControlStateNormal];
    [self.shareBtn setTitle:@" 19" forState:UIControlStateNormal];
    [self.shareBtn setTitleColor:FONTCOLOR_BLACK forState:UIControlStateNormal];
    self.shareBtn.titleLabel.font = Kfont(12);
    [self.shareBtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.shareBtn];
    
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ScreenX375(124), ScreenX375(250), ScreenX375(100), ScreenX375(14))];
    self.timeLabel.textColor = FONTCOLOR_BLACK;
    self.timeLabel.font = Kfont(12);
    self.timeLabel.textAlignment = NSTextAlignmentRight;
    self.timeLabel.text = @"10小时前";
    [self addSubview:self.timeLabel];
}

- (void)setPublishModel:(PublishModel *)publishModel {
    _publishModel = publishModel;
    
    self.noteLabel.text = publishModel.title;
    if ([publishModel.publishTime class] != [NSNull class]) {
        self.timeLabel.text = [HttpManager getTime:[HttpManager TimeStamp:publishModel.publishTime]];
    }
    [self.bgView sd_setImageWithURL:[NSURL URLWithString:publishModel.pic] placeholderImage:nil options:SDWebImageRetryFailed];
    if (!publishModel.zanNum) {
        publishModel.zanNum = @"  0";
    }
    if (!publishModel.commentNum) {
        publishModel.commentNum = @"  0";
    }
    if (!publishModel.shareNum) {
        publishModel.shareNum = @"  0";
    }
    [self.collectionBtn setTitle:publishModel.zanNum forState:UIControlStateNormal];
    [self.commentBtn setTitle:publishModel.commentNum forState:UIControlStateNormal];
    [self.shareBtn setTitle:publishModel.shareNum forState:UIControlStateNormal];
}

- (void)likeAction {
    if (_likeClick) {
        _likeClick();
    }
}

- (void)commentAction {
    if (_commentClick) {
        _commentClick();
    }
}

- (void)shareAction {
    if (_shareClick) {
        _shareClick();
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@implementation PublishModel

+(JSONKeyMapper*)keyMapper
{
    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{
                                                                  @"Id": @"id"
                                                                  }];
}


@end
